package com.avidys.gwt.wom.client;

import com.avidys.common.gwt.net.Controller;
import com.avidys.common.gwt.net.JSONRPC;
import com.avidys.common.gwt.net.Logging;
import com.avidys.common.gwt.ui.ButtonBar;
import com.avidys.common.gwt.ui.IButtonBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/*
 * Simple preliminary game interface
 */
public class Game extends Composite implements IButtonBar{
	
	String[] bs = {"Test Seq","Test Srv","Room","Unit Tests"};
	ButtonBar bb = new ButtonBar(this, bs);
	VerticalPanel vp= new VerticalPanel();

	Controller testController ;
	Controller roomController ;
	TestServerPanel testPanel; //WOMPanel
	SequentialTestServerPanel testSPanel; //WOMPanel
	GameRoomPanel roomPanel; //WOMPanel

	/**
	 * A dialog with a panel and a close button
	 * @author ca3jmaupas
	 *
	 */
	private  class TestDialogBox extends DialogBox {
		int left=20;
		int top=20;
		final int MARGIN=20;
	    public TestDialogBox(String title, Widget w) {
	    	super();
		      // Set the dialog box's caption.
		      setText(title);
		      setModal(true);
		      setPopupPosition(left, top);
		      left += MARGIN;
		      top += MARGIN;
		      VerticalPanel p = new VerticalPanel();

		      // DialogBox is a SimplePanel, so you have to set its widget property to
		      // whatever you want its contents to be.
		      Button ok = new Button("CLOSE");
		      ok.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					TestDialogBox.this.hide();
				}
		      });
		      
		      p.add(w);
		      p.add(ok);
		      setWidget(p);
		    }
	}
	
	TestDialogBox gameDialog, testDialog , testSDialog, unitTestsDialog;
	
	public Game() {
		final String[] steps = {"listServers", "createUser","login","getGames","createGame"};


		// Set the focus on the widget after setup completes.
		DeferredCommand.addCommand(new Command() {
			    public void execute () { 
			    }
		});

	      //dataEntry.setFocus();
		testController = new Controller();

		testPanel = new TestServerPanel(steps);
		testController.addControlledWidget(testPanel);
		testPanel.setController(testController); 
		testDialog = new TestDialogBox ("Testing server", testPanel);

    	testSPanel = new SequentialTestServerPanel(steps);
		testController.addControlledWidget(testSPanel);
		testSPanel.setController(testController); 
		testSDialog = new TestDialogBox ("Testing server sequentially", testSPanel);

		roomPanel = new GameRoomPanel();
		roomController = new Controller(roomPanel);
		roomPanel.setController(roomController); 
		gameDialog = new TestDialogBox ("Testing game room", roomPanel);
		
		VerticalPanel p = new VerticalPanel();
		p.add(Logging.getPanel());
		unitTestsDialog = new TestDialogBox ("Unit Testing", p);

		
		vp.add(bb);
		initWidget(vp);
	}

	public void onButtonClicked(int lastButtonClickedInt,
			Button lastButtonClicked) {
		// GWT.log(""+lastButtonClickedInt,null);
		if (lastButtonClickedInt == 0) testSDialog.show();
		if (lastButtonClickedInt == 1) testDialog.show();
		if (lastButtonClickedInt == 2) gameDialog.show();
		if (lastButtonClickedInt == 3) {
			unitTestsDialog.show();
			JSONRPC.test();
		}
	}
	

}
